/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.gateway.configurer.exception;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.codec.ServerCodecConfigurer;

import java.util.Map;

/**
 * @author Arisgo
 * @see ErrorWebFluxAutoConfiguration
 * 自动注册，如果有实现ErrorWebExceptionHandler接口的类，则DefaultErrorWebExceptionHandler不注册
 * 调用 @see {@link org.springframework.web.server.handler.ExceptionHandlingWebHandler} 中的 handle
 * 顺序  ExceptionHandlingWebHandler.CheckpointInsertingHandler
 * DefaultErrorWebExceptionHandler (ArisgoFluxGlobalExceptionHandler)
 * WebFluxResponseStatusExceptionHandler
 * <p>
 * 支持 @ExceptionHandler注解
 * 注入 @see {@link org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration} 调用 {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder}
 * @since 2024/6/8
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ServerProperties.class, WebProperties.class})
public class ArisgoGatewayExceptionHandler extends DefaultErrorWebExceptionHandler implements Ordered {

    public ArisgoGatewayExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties,
                                         ServerProperties serverProperties, ApplicationContext applicationContext,
                                         ServerCodecConfigurer serverCodecConfigurer) {
        super(errorAttributes, webProperties.getResources(), serverProperties.getError(), applicationContext);
        super.setMessageWriters(serverCodecConfigurer.getWriters());
        super.setMessageReaders(serverCodecConfigurer.getReaders());
    }

    @Override
    protected int getHttpStatus(Map<String, Object> errorAttributes) {
        return (int) errorAttributes.get("code");
    }

    @Override
    public int getOrder() {
        return -1;
    }
}
